using System;
using System.Drawing;
using System.Drawing.Imaging;

    /// <summary>
	/// Summary description for ImageInfo.
	/// </summary>
	public class ImageInfo
	{
		public int Width { get { return m_bmpRepresentation.Width; } }
		public int Height { get { return m_bmpRepresentation.Height; } }
		public Bitmap ImageFile { get { return m_bmpRepresentation; } }
		protected Bitmap m_bmpRepresentation;

		public string Format
		{
			get 
			{
				ImageFormat bmpFormat = m_bmpRepresentation.RawFormat;
				string strFormat = "unidentified format";
      
				if (bmpFormat.Equals(ImageFormat.Bmp)) strFormat = "BMP";
				else if (bmpFormat.Equals(ImageFormat.Emf)) strFormat = "EMF";
				else if (bmpFormat.Equals(ImageFormat.Exif)) strFormat = "EXIF";
				else if (bmpFormat.Equals(ImageFormat.Gif)) strFormat = "GIF";
				else if (bmpFormat.Equals(ImageFormat.Icon)) strFormat = "Icon";
				else if (bmpFormat.Equals(ImageFormat.Jpeg)) strFormat = "JPEG";
				else if (bmpFormat.Equals(ImageFormat.MemoryBmp)) strFormat = "MemoryBMP";
				else if (bmpFormat.Equals(ImageFormat.Png)) strFormat = "PNG";
				else if (bmpFormat.Equals(ImageFormat.Tiff)) strFormat = "TIFF";
				else if (bmpFormat.Equals(ImageFormat.Wmf)) strFormat = "WMF";
      
				return strFormat;
			}
		}

		public ImageInfo()
		{

		}

		public ImageInfo(string strImageFile)
		{
			try 
			{
				m_bmpRepresentation = new Bitmap(strImageFile, false);
			} 
			catch 
			{
			}
		}

		public void Load(string strImageFile) 
		{
			try 
			{
				m_bmpRepresentation = new Bitmap(strImageFile, false);
			} 
			catch 
			{
			}
		}

		public virtual void Dispose()
		{
			m_bmpRepresentation.Dispose();
		}

        /// <summary>
        /// If a thumbnail file exists for the given image the thumbnail filename is returned. Default width is 75.
        /// </summary>
        /// <param name="imageFilename"></param>
        /// <param name="RelativeWebPath"></param>
        /// <returns></returns>
        public static bool getThumbnailName(string imageFilename, string RelativeWebPath, out string ThumbnailFileName)
        {
            return getThumbnailName(imageFilename, RelativeWebPath, 75, out ThumbnailFileName);
        }
        /// <summary>
        /// Check for a thumbnail file of the given size and return its filename.
        /// </summary>
        /// <param name="imageFilename"></param>
        /// <param name="RelativeWebPath"></param>
        /// <param name="DesiredWidth"></param>
        /// <returns></returns>
        public static bool getThumbnailName(string imageFilename, string RelativeWebPath, int DesiredWidth, out string ThumbnailFileName)
        {
            ThumbnailFileName = "";
            if (imageFilename == null || RelativeWebPath == null || DesiredWidth == null)
                return false;
            System.Web.HttpContext context = System.Web.HttpContext.Current;
            string[] fileParts = imageFilename.Split(char.Parse("."));
            if (fileParts.Length > 1)
            {
                if (System.IO.File.Exists(context.Server.MapPath(RelativeWebPath + fileParts[fileParts.Length - 2] + "_" + DesiredWidth.ToString() + "." + fileParts[fileParts.Length - 1])))
                {
                    ThumbnailFileName = fileParts[fileParts.Length - 2] + "_" + DesiredWidth.ToString() + "." + fileParts[fileParts.Length - 1];
                    return true;
                }
            }
            return false;
        }

	}

	public class ImageSizer : ImageInfo 
	{
		public double tempMultiplier;
		public ImageSizer(string strImageFile) 
		{
			try 
			{
				this.m_bmpRepresentation = new Bitmap(strImageFile, false);
			} 
			catch
			{
				this.m_bmpRepresentation = null;
			}
		}

		public Size getNewSize(int desiredMaxWidth, int desiredMaxHeight) 
		{
			if (this.Height > this.Width) 
			{ // portrait
				tempMultiplier = desiredMaxHeight / System.Convert.ToDouble(Height);
			} 
			else 
			{
				tempMultiplier = desiredMaxWidth / System.Convert.ToDouble(Width);
			}

			double newWidth = Width * tempMultiplier;
			double newHeight = Height * tempMultiplier;

			if (newHeight > desiredMaxHeight) 
			{ 
				tempMultiplier = desiredMaxHeight / System.Convert.ToDouble(newHeight);
				newWidth = newWidth * tempMultiplier;
				newHeight = newHeight * tempMultiplier;
			} 
			else if (newWidth > desiredMaxWidth) 
			{
				tempMultiplier = desiredMaxWidth / System.Convert.ToDouble(newWidth);
				newWidth = newWidth * tempMultiplier;
				newHeight = newHeight * tempMultiplier;
			}

			Size adjustedSize = new Size(System.Convert.ToInt32(newWidth), System.Convert.ToInt32(newHeight));

			return adjustedSize;
		}
	
		public override void Dispose()
		{
			base.Dispose ();
		}

		
	}
